<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="/res/back/css/base.css" rel="stylesheet" type="text/css" />
<link href="/res/third/bootstrap3/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="/res/common/js/jquery.1.9.1.min.js" type="text/javascript"></script>
<script src="/res/third/bootstrap3/jsone/bootstrap.min.js" type="text/javascript"></script>
<script src="/res/third/artDialog/jquery.artDialog.js" type="text/javascript"></script>
<script src="/res/third/validate/jquery.validate.js" type="text/javascript"></script>
<script src="/res/third/layer/layer.js"></script>
<script type="text/javascript">
	function sosuo(){
		var lpath =location.pathname;
		//var pageNo = $("#pageNo").val();
		$("#soform").attr("action",lpath+"?pageNo="+1);
		$("#soform").submit();
	}

	$(document).ready(function(){
		$("#fathCheck").click(function(){
			$(".giftInfoIds").prop("checked",this.checked);
		})
	})
	
	function plchange(){
		var ids="";
    	var len=$(".giftInfoIds:checked").length;
    	if(len==0){
    		alert("请选择要修改的元素");
    		return false;
    	}
    	$(".giftInfoIds:checked").each(function(index){
    		if(index!=(len-1)){
    			ids+=$(this).val()+",";
			}else{
				ids+=$(this).val();
			}
    	})
    	art.dialog.post({
  		  
    		type : "post",
    		dataType : "json",
    		url : "/gift/plchange.do",
            data:{ids:ids},
    		success : function(data) {
    			 if(data.success){
    				 location.reload();
    			 }else{
    				 alert(data.msg);
    			 }
    		}
     
    		},"数据正在提交.... ");
	}
	
	function pldelete(){
			if(confirm("确认删除吗？")){
				var ids="";
		    	var len=$(".giftInfoIds:checked").length;
		    	if(len==0){
		    		alert("请选择要删除的元素");
		    		return false;
		    	}
		    	$(".giftInfoIds:checked").each(function(index){
		    		if(index!=(len-1)){
		    			ids+=$(this).val()+",";
					}else{
						ids+=$(this).val();
					}
		    	})
		    	art.dialog.post({
		  		  
		    		type : "post",
		    		dataType : "json",
		    		url : "/gift/pldelete.do",
		            data:{ids:ids},
		    		success : function(data) {
		    			 if(data.success){
		    				 location.reload();
		    			 }else{
		    				 alert(data.msg);
		    			 }
		    		}
		     
		    		},"数据正在提交.... ");
			}
	}
	
	function changeGiftState(gid,node){
		art.dialog.post({
	  		type : "post",
    		dataType : "json",
    		url : "/gift/updateOne.do",
            data:{id:gid},
    		success : function(data) {
    			 if(data.success){
    				 alert("操作成功");
    				 if($(node).text()=="开启"){
    					 $(node).text("暂停");
    					 $(node).parents("tr").find("td").eq(8).text("开启兑换")
    				 }else{
    					 $(node).text("开启");
    					 $(node).parents("tr").find("td").eq(8).text("暂停兑换")
    				 }
    			 }else{
    				 alert(data.msg);
    			 }
    		}
     
    		},"数据正在提交.... ");
	}
</script>
<style type="text/css">
	table{table-layout: fixed;}
	td{overflow: hidden;text-overflow:ellipsis;}
	.excelbtn{display: inline-block;height: 100%;width: 100px;background-color:#38A4C7;color: #fff;text-align: center;line-height: 35px;cursor: pointer;border-radius:5px;}
	.bcksty{width: 700px; height: 300px; display: none;font-size: 13px;font-weight: bold;padding: 0px 12px;}
	.title_gift{text-align: left;padding-top: 30px;border-bottom: 1px solid #ddd;}
	.title_gift_text{display: inline-block;margin-left: 10px;border-bottom: 3px solid #E64545;font-size: 14px;width: 80px;text-align: center;}
	.title_info_font{}
	.jl_title {
	    width: 100%;
	    display: inline-block;
	    height: 38px;
	    border-top: 1px solid #ddd;
	    border-bottom: 1px solid #ddd;
	    background-color: #F5F5F5;
	 }
	.jl_title_text ,.jl_stock_text{
	    width: 50%;
	    display: inline-block;
	    float: left;
	    line-height: 38px;
	    text-align: center;
	}
	.jl_stock{
		width: 100%;
	    display: inline-block;
	    height: 38px;
	    border-bottom: 1px solid #ddd;
	}
</style>
</head>

<body>
	<div class="panel panel-info panel-content">
	<div class="panel-heading" style="position: relative;">
			<button style="float: right" type="button" onclick="addgift()" class="btn btn-info">
				<span class="glyphicon glyphicon-plus"></span>
				&nbsp;新增礼品
			</button>
			<form id="soform" method="post">
				<table>
					<tr>
						<td class="w80" style="padding-top: 5px">
							<label for="giftName" class="center-block">礼品名称：</label>
						</td>
						<td class="w150 ">
							<input type="text" class="form-control" name="so_giftName" value="${so_giftName!}" id="so_search" placeholder="礼品名称" data-options="">
						</td>
						<td class="w80" style="padding-top: 5px;">
							<label for="companyname" class="center-block">兑换状态：</label>
						</td>
						<td class="w120 ">
							<select class="form-control" name="so_giftState">
							    <option value="" [#if !so_giftState?? || so_giftState=='']selected="selected"[/#if]>全部</option>
								<option value="0" [#if so_giftState?? && so_giftState=='0']selected="selected"[/#if]>暂停兑换</option>
								<option value="1" [#if so_giftState?? && so_giftState=='1']selected="selected"[/#if]>开放兑换</option>
							</select>
						</td>
						<td style="padding-left: 10px">
							<div class="input-group">
								<span class="input-group-btn" style="border: 1px solid #ddd;">
									<button class="btn btn-default" type="button" onclick="sosuo()">搜索</button>
								</span>
							</div>
						</td>
					</tr>
				</table>
		</form>
		</div>
		<table class="table table-hover table-bordered table-striped table-condensed" style="margin-bottom: 8px!important;">
			<thead>
				<tr>
					<th width="5%"><input type="checkbox" id="fathCheck"></th>
					<th width="5%">序号</th>
					<th width="12%">礼品编号</th>
					<th width="16%">礼品名称</th>
					<th width="8%">排序号</th>
					<th width="8%">库存</th>
					<th width="10%">兑换积分</th>
					<th width="11%">提供方</th>
					<th width="10%">状态</th>
					<th width="15%">操作</th>
				</tr>
			</thead>
			[#list pagination.list as gift]
			<tr>
				<td align="center"><input type="checkbox" class="giftInfoIds" value="${gift.id!}"></td>
				<td align="center">${gift_index+1!}</td>
				<td align="center">${gift.giftCode!}</td>
				<td align="center">${gift.giftName!}</td>
				<td align="center">${gift.seq!}</td>
				<td align="center">${gift.stockAmount!}</td>
				<td align="center">${gift.integralNum!}</td>
				<td align="center">${gift.supportCompany!}</td>
				<td class="ztext" align="center">
						[#if gift.state?? && gift.state==0]暂停兑换[/#if]
						[#if gift.state?? && gift.state==1]开放兑换[/#if]
				</td>
				<td> 
				 <button type="button" class="btn btn-info btn-xs delbtn" onclick="editGiftInfo('${gift.id!}')">编辑</button>
				 [#if gift.state?? && gift.state==0]
				 	<button type="button" class="btn btn-success btn-xs delbtn" onclick="changeGiftState('${gift.id!}',this)">开启</button>
				 [/#if]
				 [#if gift.state?? && gift.state==1]
				 	<button type="button" class="btn btn-success btn-xs delbtn" onclick="changeGiftState('${gift.id!}',this)">暂停</button>
				 [/#if]
				 <button type="button" class="btn btn-primary btn-xs delbtn" onclick="sellDetail('${gift.id!}','${gift.giftCode!}','${gift.stockAmount!}')">补充库存</button>
				</td>
			</tr>
			[/#list]
		</table>
		<div style="display: inline-block;width: 100%;">
			<button type="button" onclick="plchange()" class="btn btn-primary btn-sm">批量暂停/开始</button>
			<button type="button" onclick="pldelete()" class="btn btn-primary btn-sm">批量删除</button>
		</div>
		<div class="panel-footer">[@fenye p = pagination form = 'infoForm' /]</div>
	</div>
	
	<div class="bcksty" id="bckudiv">
		 <div class="title_gift">
			<span class="title_gift_text">补充库存</span>
		 </div>
		 <div style="margin-top: 20px;">
		 	<span class="title_info_font" style="margin-left: 20px;">商品编号：</span>
		 	<span id="l_giftCode" class="title_info_font"></span>
		 	<span class="title_info_font" style="margin-left: 20px;">当前剩余库存：</span>
		 	<span id="l_stockAmount" class="title_info_font"></span>
		 	<span class="title_info_font" style="margin-left: 20px;">补充数量：</span>
		 	<span class="title_info_font">
		 		<input id="l_addAmount" type="text" style="width: 65px;">
		 	</span>
		 	<span class="title_info_font btn btn-primary btn-sm" style="cursor: pointer;"  onclick="addAmount()">确认</span>
		 </div>
		 <div class="title_gift" style="padding-top: 15px;">
			<span class="title_gift_text">补充记录</span>
		 </div>
		 <div style="width: 100%;height: 178px;margin-top: 20px;">
		 	 <div class="jl_title">
		 	 	<span class="jl_title_text">补充时间</span>
		 	 	<span class="jl_title_text">补充数量</span>
		 	 </div>
		 	 <div class="jl_stock">
		 	 	<span class="jl_stock_text">2017-02-17 09:53:04</span>
		 	 	<span class="jl_stock_text">1</span>
		 	 </div>
		 </div>
	</div>

    <script src="/res/common/js/moment.min.js"></script>
<script type="text/javascript">
function addgift(){
	top.addTempTab("/gift/giftInfo_add.do");
}

function editGiftInfo(id){
	top.addTempTab("/gift/giftInfo_edit.do?id="+id);
}

var gid;
function sellDetail(id,giftCode,stockAmount){
	var layerIndex;
	layer.open({
		  type:1,
		  btn:['取消'],
		  area: ['720px', '460px'],
		  title: '补充库存',
		  content: $("#bckudiv"),
		  success: function(layero, index){
			    layerIndex=index;
			    $(layero).find("#l_giftCode").text(giftCode);
			    $(layero).find("#l_stockAmount").text(stockAmount);
			    $(layero).find(".jl_title").siblings(".jl_stock").remove();
			    gid=id;
			    art.dialog.post({
					  
					type : "post",
					dataType : "json",
					async: false,
					url : "/gift/giftStockList.do",
					data : {id:id},
					success : function(data) {
						 if(data.success){
							 if(data.obj){
								 for (var i = 0; i < data.obj.length; i++) {
									$(".jl_title").after("<div class='jl_stock'>"
												 	 	+"<span class='jl_stock_text'>"+moment(data.obj[i].createtime).format('YYYY-MM-DD hh:mm:ss')+"</span>"
												 	 	+"<span class='jl_stock_text'>"+data.obj[i].amount+"</span>"
												 	    +"</div>")
								 }
							 }
						 }else{
							 alert(data.msg);
						 }
					}
			 
						},"数据正在加载.... ");
			  }
		});     
}

function addAmount(){
	var addAmount=$("#l_addAmount").val().trim();
	var t=/^[0-9]*[1-9][0-9]*$/;
	if(!t.test(addAmount)){
		alert("增加个数必须为正整数！")
		return false;
	}
	 art.dialog.post({
		  
			type : "post",
			dataType : "json",
			async: false,
			url : "/gift/addGiftStock.do",
			data : {giftId:gid,amount:addAmount},
			success : function(data) {
				 if(data.success){
					location.reload();
				 }else{
					 alert(data.msg);
				 }
			}
	 
				},"数据正在加载.... ");
}
</script>
</body>
</html>